//
//  AlertControl.m
//  Opper
//
//  Created by Le Dung on 4/16/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "AlertControl.h"
@interface AlertControl ()

@end

@implementation AlertControl
@synthesize dataArray;
@synthesize isSetAlert;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    [StyleClass barButtonItemEdit:self left:NO right:YES];
}

-(void)rightPress
{
    

}




-(void)cancelPress
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)buttonTarget
{
    [self.acceptButton addTarget:self action:@selector(acceptPress) forControlEvents:UIControlEventTouchUpInside];
    [self.acceptButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setBackgroundImage:[StyleClass nRed] forState:UIControlStateNormal];
    
    [self.listDay setBackgroundImage:[StyleClass nList] forState:UIControlStateNormal];
    self.listDay .contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    self.listDay .contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.listDay addTarget:self action:@selector(listDayPress) forControlEvents:UIControlEventTouchUpInside];
    
    self.textAlert.placeholder =@" Text Alert";
    self.textAlert.borderStyle =UITextBorderStyleNone;
    [self.textAlert setBackground:[StyleClass nText]];
    self.textAlert.delegate =self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self buttonTarget];
    [StyleClass createNavi:self andTitle:@"Autoverzekering"];
    [StyleClass backgroundInblack:self.view];
    [self styleContaint];

    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)styleContaint
{
    [StyleClass background:self.containView];
    self.containView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.containView.layer.shadowOffset = CGSizeMake(0.0, 2.0);
    self.containView.layer.shadowOpacity = 1.0f;
    
    self.datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
    [self.containView addSubview:self.datePicker];
    [self.datePicker setHidden:YES];
    [self.datePicker addTarget:self action:@selector(dateChange) forControlEvents:UIControlEventValueChanged];
    [self.containView addSubview:self.datePicker];
    
    
    CGRect frame =self.listDay.frame;
    frame.size.width=280;
    frame.origin.x=10;
    frame.origin.y=frame.origin.y+30;
    self.datePicker.frame =frame;
}


-(void)listDayPress
{
    self.datePicker.hidden?[self.datePicker setHidden:NO] : [self.datePicker setHidden:YES];
    
}
-(void)dateChange
{
        NSDate *currDate = [self.datePicker date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"dd-MM-YYYY"];
        NSString *dateString = [dateFormatter stringFromDate:currDate];
        [self.listDay setTitle:dateString forState:UIControlStateNormal];

}


- (void)setReminder
{
    if (self.eventStore == nil)
    {
        self.eventStore = [[EKEventStore alloc]init];
        
        [self.eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
            
            if (!granted)
                NSLog(@"Access to store not granted");
        }];
    }
    
    if (self.eventStore != nil)
        [self createReminder];
}

-(void)acceptPress
{
    
    
    [self setReminder];
    
    //ContractAddNew *page =[[[ContractAddNew alloc]init] autorelease];
    //[self.navigationController popViewControllerAnimated:YES];
    
}

-(void)createReminder
{
    EKReminder *reminder = [EKReminder
                            reminderWithEventStore:self.eventStore];
    
    reminder.title = self.textAlert.text;
    reminder.calendar = [self.eventStore defaultCalendarForNewReminders];

    
    NSDate *date = [self.datePicker date];
    
    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
    
    [reminder addAlarm:alarm];
    
    NSError *error = nil;
    
    [self.eventStore saveReminder:reminder commit:YES error:&error];
    
    if (error)
        NSLog(@"error = %@", error);
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_containView release];
    [_acceptButton release];
    [_cancelButton release];
    [_listDay release];
    [_textAlert release];
    [super dealloc];
}
@end
